<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title data-i18n="resources.title_layerGroup"></title>
    <style type="text/css">
        .winTitle {
            background: #4192c9;
            height: 20px;
            line-height: 20px
        }

        .winTitle .title_left {
            font-weight: bold;
            color: #FFF;
            padding-left: 5px;
            float: left
        }

        .winTitle .title_right {
            float: right;
            padding-right: 3px;
        }

        .winTitle .title_right a {
            color: #FFF;
            text-decoration: none;
            padding-right: 3px;
        }

        .winTitle .title_right a:hover {
            text-decoration: underline;
            color: #FF0000;
            padding-right: 3px;
        }

        .winContent {
            padding: 5px;
            overflow-y: auto;
            height: 550px;
        }

        .popupWindow {
            right: 20px;
            top: 170px;
            position: absolute;
            border: 2px solid #D6E3F1;
            background: #FFF;
            z-index: 9999;
        }

        .editPane {
            position: absolute;
            right: 60px;
            top: 50px;
            text-align: center;
            background: #FFF;
            z-index: 1000;
        }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id='result' class='container'></div>

<div class='panel panel-primary editPane' id='editPane' style="z-index: 99999">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.text_layerGroup"></h5>
    </div>
    <div class='panel-body' id='params'>
        <p></p>
        <div align='center' class='button-group'>
            <input type='button' id='btn1' class='btn btn-primary' data-i18n="[value]resources.btn_layerGroup"
                   onclick="show()"/>
        </div>
    </div>
</div>
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<script type="text/javascript" include="bootstrap,zTree,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script type="text/javascript">
    var map = null;
    var layer = null;
    var tempLayerID = null,
        host = window.isLocal ? window.server : "http://support.supermap.com.cn:8090",
        url = host + "/iserver/services/map-jingjin/rest/maps/京津地区地图";

    // 获取子图层状态信息
    function getLayersInfo() {
        var getLayersInfoService = new SuperMap.REST.GetLayersInfoService(url);
        getLayersInfoService.events.on({"processCompleted": getLayersInfoCompleted});
        getLayersInfoService.processAsync();
    }

    getLayersInfo();

    //与服务器交互成功，得到子图层信息
    function getLayersInfoCompleted(getLayersInfoEventArgs) {
        if (getLayersInfoEventArgs.result) {
            {
                handleTreeData(getLayersInfoEventArgs.result.subLayers.layers, 1);
                showWindow();
                $.fn.zTree.init($("#tree"), setting, treeNodes);
            }
        }
        createTempLayer();
    }

    //图层组控制菜单的配置
    var setting = {
        view: {
            dblClickExpand: false,
            showLine: false,
            showIcon: false,
            selectedMulti: false
        },
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: "0"
            }
        },
        callback: {
            beforeClick: function (treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj("tree");
                if (treeNode.isParent) {
                    zTree.expandNode(treeNode);
                    return false;
                }
            },
            onCheck: setLayerStatus
        }
    };

    var treeNodes = [];
    var layerID = 0;

    //处理layers中的子图层信息为 ztree所需的数据格式
    function handleTreeData(layers) {
        var length = layers.length;
        for (var i = 0; i < length; i++) {
            var node = {};
            node.id = i + 1;
            node.pId = 0;
            node.name = layers[i].name;
            node.open = true;
            node.isChild = false;
            if (layers[i].visible) {
                node.checked = true;
            }
            treeNodes.push(node);

            if (layers[i].subLayers.layers && layers[i].subLayers.layers.length > 0) {
                for (var j = 0; j < layers[i].subLayers.layers.length; j++) {
                    var node = {};
                    node.id = (i + 1) * 10 + j;
                    node.pId = i + 1;
                    node.name = layers[i].subLayers.layers[j].name;
                    node.isChild = true;
                    node.layerID = j;
                    layerID++;
                    if (layers[i].subLayers.layers[j].visible) {
                        node.checked = true;
                    }
                    treeNodes.push(node);
                }
            }
        }

    }

    //创建临时图层来初始化当前地图显示
    function createTempLayer() {
        //子图层控制参数必设：url、mapName、SetLayerStatusParameters
        var layerStatusParameters = new SuperMap.REST.SetLayerStatusParameters();
        layerStatusParameters = getLayerStatusList(layerStatusParameters);

        var setLayerStatusService = new SuperMap.REST.SetLayerStatusService(url);
        setLayerStatusService.events.on({"processCompleted": createTempLayerCompleted});
        setLayerStatusService.events.on({"processFailed": createTempLayerFailed});
        setLayerStatusService.processAsync(layerStatusParameters);
    }

    //获取当前地图子图层状态信息
    function getLayerStatusList(parameters) {
        for (var i = 0; i < treeNodes.length; i++) {
            if (treeNodes[i].isChild) {
                var layerStatus = new SuperMap.REST.LayerStatus();
                layerStatus.layerName = treeNodes[i].name;
                layerStatus.isVisible = false;
                parameters.layerStatusList.push(layerStatus);

            }
        }
        //设置资源在服务端保存的时间，单位为分钟，默认为10
        parameters.holdTime = 30;
        return parameters;
    }

    //与服务器交互成功，创建临时图层
    function createTempLayerCompleted(createTempLayerEventArgs) {
        tempLayerID = createTempLayerEventArgs.result.newResourceID;
        //创建地图控件
        map = new SuperMap.Map("map", {
            controls: [
                new SuperMap.Control.ScaleLine(),
                new SuperMap.Control.Zoom(),
                new SuperMap.Control.Navigation({
                    dragPanOptions: {
                        enableKinetic: true
                    }
                })]
        });

        //创建 TiledDynamicRESTLayer
        layer = new SuperMap.Layer.TiledDynamicRESTLayer("World", url, {
            transparent: true,
            cacheEnabled: false,
            redirect: true,
            layersID: tempLayerID
        }, {maxResolution: "auto", bufferImgCount: 0});
        layer.bufferImgCount = 0;
        layer.events.on({"layerInitialized": addLayer});
    }

    //显示错误信息
    function createTempLayerFailed(result) {
        widgets.alert.showAlert(result.error.errorMsg, false);
    }

    function addLayer() {
        var center = new SuperMap.LonLat(117, 40);
        map.addLayers([layer]);
        map.setCenter(center, 0);
    }

    //通过子图层layersID可见性控制
    function setLayerStatus() {
        var zTree = $.fn.zTree.getZTreeObj("tree"),
            checkCount = zTree.getCheckedNodes(true);
        var checkLength = checkCount.length;

        //通过layersID 控制图层的显示和隐藏   目前iserver还有问题
        var str = "[0:";
        for (var i = 0; i < checkLength; i++) {
            if (checkCount[i].isChild) {
                if (i < checkLength) {
                    str += (checkCount[i].pId - 1).toString() + "." + checkCount[i].layerID.toString();
                }
                if (i < (checkLength - 1)) {
                    str += ",";
                }
            }
        }

        str += "]";
        //当所有图层都不可见时
        if (str.length < 5) {
            str = "[]";
        }
        layer.params.layersID = str;
        layer.redraw();
    }

    function showWindow() {
        if (document.getElementById("popupWin")) {
            $("#popupWin").css("display", "block");
        } else {
            $("<div id='popupWin'></div>").addClass("popupWindow").appendTo($("#result"));
        }
        $("#popupWin").css("display", "none");
        var str = "";
        str += '<div class="winTitle" onMouseDown="startMove(this,event)" onMouseUp="stopMove(this,event)"><span class="title_left">' + resources.text_JingjinMap + '</span><span class="title_right"><a href="javascript:closeWindow()" data-i18n="[title]resources.text_closeWindow">' + resources.text_close + '</a></span><br style="clear:right"/></div>';  //标题栏

        str += '<div class="winContent" style="overflow-y:auto;height:440px;">';
        str += '<ul id="tree" class="ztree"></ul>';
        str += '</div>';
        $("#popupWin").html(str);
        document.getElementById("popupWin").style.width = "260px";
        document.getElementById("popupWin").style.height = "472px";
    }

    window.closeWindow = function () {
        $("#popupWin").css("display", "none");
    };
    window.startMove = function (o, e) {
        var wb;
        if (SuperMap.Browser.name === "msie" && e.button === 1) wb = true;
        else if (e.button === 0) wb = true;
        if (wb) {
            var x_pos = parseInt(e.clientX - o.parentNode.offsetLeft);
            var y_pos = parseInt(e.clientY - o.parentNode.offsetTop);
            if (y_pos <= o.offsetHeight) {
                document.documentElement.onmousemove = function (mEvent) {
                    var eEvent = (SuperMap.Browser.name === "msie") ? event : mEvent;
                    o.parentNode.style.left = eEvent.clientX - x_pos + "px";
                    o.parentNode.style.top = eEvent.clientY - y_pos + "px";
                }
            }
        }
    };
    window.stopMove = function (o, e) {
        document.documentElement.onmousemove = null;
    };

    function show() {
        $("#popupWin").css("display", "block");
    }
</script>
</body>
</html>
